<template>
  <div>
    <div class="display-flex justify-content-flex-justify left-title">
      <div>{{ titleLeft }}</div>
      <div>
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
        <el-button
          type="primary"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          >删除</el-button
        >
      </div>
    </div>

    <el-table
      border
      :cell-style="{ padding: this.tablePadding }"
      :data="setSelectItems"
      @selection-change="handleSelectionChangeSet"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="性能名称" align="center" prop="paramName" />
      <el-table-column
        v-if="isShow"
        label="性能分类"
        align="center"
        prop="paramCategoryName"
      />
      <el-table-column
        v-if="isShow"
        label="单位"
        align="center"
        prop="unitName"
      />
      <el-table-column label="值" align="center" prop="value" width="130">
        <template slot-scope="scope">
          <el-input
            v-model="scope.row.value"
            placeholder="请输入值"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </template>
      </el-table-column>
      <el-table-column
        v-if="isShow"
        label="描述"
        align="center"
        prop="valueStr"
        width="140"
      >
        <template slot-scope="scope">
          <el-input
            v-model="scope.row.valueStr"
            placeholder="请输入描述"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        v-if="isShow"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row.paramId)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        label-width="68px"
      >
        <el-form-item label="参数名称" prop="paramName">
          <el-input
            v-model="queryParams.paramName"
            placeholder="请输入名称"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
      <el-table
        :cell-style="{ padding: this.tablePadding }"
        v-loading="loading"
        :data="paramList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />

        <el-table-column label="性能名称" align="center" prop="paramName" />
        <el-table-column
          label="性能分类"
          align="center"
          prop="paramCategoryName"
        />
        <el-table-column label="单位" align="center" prop="unitName" />
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>



<script>
import { listParam } from "@/api/base/dataType";
import { removeByValue } from "@/utils/index";
const configId = require("@/utils/configId");
export default {
  props: {
    titleLeft: {
      type: String,
      default: "材料信息",
    },
    isShow: {
      type: Boolean,
      default: true,
    },
  },
  data() {
    return {
      title: "选择性能参数",
      multiple: true,
      multipleSet: true,
      paramList: [],
      loading: false,
      form: {},
      open: false,
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        paramName: null,
        paramCategoryId: configId.paramCategoryId,
      },
      ids: [],
      selectItems: [], //选中的参数
      setSelectItems: [],
    };
  },
  methods: {
    setTable(data) {
      this.setSelectItems = data.map((item) => {
        return item;
      });
    },
    submitForm() {
      if (this.setSelectItems.length == 0) {
        this.setSelectItems = JSON.parse(JSON.stringify(this.selectItems));
        this.cancel();
        return;
      }
      let arr = this.selectItems.filter(
        (item) =>
          !this.setSelectItems.some((ele) => ele.paramId === item.paramId)
      );
      arr.forEach((item) => {
        this.setSelectItems.push(item);
      });
      this.cancel();
    },
    resetQuery() {
      this.queryParams.paramName = "";
      this.handleQuery();
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    handleAdd() {
      this.open = true;
      this.getList();
    },
    handleDelete(id) {
      removeByValue(this.setSelectItems, "paramId", id);
    },
    handleDeleteSelect() {
      this.ids.forEach((item) => {
        removeByValue(this.setSelectItems, "paramId", item);
      });
    },
    cancel() {
      this.open = false;
      this.selectItems = [];
    },
    handleSelectionChangeSet(selection) {
      this.ids = selection.map((item) => item.paramId);

      this.multipleSet = !selection.length;
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.selectItems = selection.map((item) => {
        return { ...item, value: "", valueStr: "" };
      });
      this.multiple = !selection.length;
    },

    /** 查询参数定义列表 */
    getList() {
      this.loading = true;
      listParam(this.queryParams).then((response) => {
        this.paramList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
  },
};
</script>
<style scoped>
.left-title {
  padding-bottom: 20px;
  font-size: 14px;
  color: #606266;
  font-weight: bold;
}
</style>